<?php
//begin session
require 'session.php';

//Check if logged in
include 'loginoptional.php';

if (!empty($_GET['char']))
	//form has results, so look up character
{
	//connect to database
	$con = require "connect.php";
	if (!$con)
	{
		die('Could not connect to database: ' . mysqli_error());
	}

	//extract realm and character data
	$realm = stripslashes(stripslashes($_GET['realm']));
	$char = stripslashes(stripslashes($_GET['char']));
	//fix case - convert to lowercase and initcaps
	$realm = ucwords(strtolower($realm));
	$char = ucwords(strtolower($char));

	//set session variables
	$_SESSION['realm']=$realm;
	$_SESSION['char']=$char;

	//include variables (API connection is in these files too)
	include 'functions.php';
	include 'vars.php';//exception is generated here

	//Allow adding character to account if logged in
	echo "<p>";
	if (@$_SESSION['auth'] == "yes")
	{
		include 'qrs.php';

		if (!isset($char_id))
		{}
		//if the character is already being watched
		if (mysqli_num_rows($sqlSelectChar1) > 0)
		{
			echo "<br/><span class='add'>Already watching character";
			echo "</span><br/>";
		}
		//if the character is not being watched and has not been added
		else if (empty($_POST['add']))
			{
				echo "<span class='add'><form name='addform' method='post'>";
				echo "<input type='hidden' name='add' value='1'/>";
				echo "<input type='hidden' name='charsearch' value='1'/>";
				echo "<input type='hidden' name='realm' value='$realm'/>";
				echo "<input type='hidden' name='char' value='$char'/>";
				//charSet set again to bring in the other form results
				echo "<input type='hidden' name='charSet' value='1'/>";
				echo "<input class='fakebutton add' name='charadd' type='submit' value='Watch this character'/>";
				echo "</form></span><br/>";
			}

		else if (!empty($_POST['add']))//the character has just been added
			{
				mysqli_query($mysqli,$sqlInsertUserChar);
				echo "<br/><br/><span class='add'>Now watching this character";
				echo "</span><br/><br/>";
			}
		echo "</p>";
	}

	//display character picture as a link to armory
	echo $armoryUrl;
	echo "<br/>";
	echo "<br/>";
	
	//insert database records
	//insert character record if it can't be found
	include 'qrs.php';
	if ($sqlNumCharRow<1)
	{
		mysql_query_($sqlInsertChar);
		if (!$sqlInsertChar)
		{
			echo "<p>Your character couldn't be added to the database: </p>" . mysqli_error($mysqli);
		}
		//insert first history record
		include 'qrs.php';
		//if there is no history record, create one
		if ($sqlHistRow < 1)
		{
			mysql_query_($sqlCreateHistory);
			if (!$sqlCreateHistory)
			{
				echo mysqli_error($mysqli);
			}
		}
	$levelLink=$level;//display number instead of link	
	}
	else
	{
	//encode the realm name to allow links to work
	$realmSpecial = urlencode($realm);
	echo "<p>We haz all your datas!<br/>";//if data is present, show link to level graph
	$lvlGraph = 'Click here for a graph of your level history!';
	$levelLink = "<a href='graph.php?char=$char&realm=".$realmSpecial."&type=level&ref=index'>$level</a>";
	$ilvlGraph = 'Click here for a graph of your item level history!';
	$ilevelLink = "<a href='graph.php?char=$char&realm=$realmSpecial&type=ilevel&ref=index'>$itemLevel</a>";
	}
	//DRILL DOWN and tracking
	//Add all characters from guild (not dependent on data)
	if ($guildName<>''&&$online==1)
	{
	$guild = $guildName;
	$guildName = "<a title='click here to start recording data for everyone in your guild!' class='table' href='guildadd.php?realm=$realmSpecial&guild=$guildName'>$guildName</a>";
	}
	//build link to achievements page
	if ($points > 0)
	{
	$points = "<a href='achievements.php?char=$char&realm=$realmSpecial' title='Click here for more information on achievements'>$points</a>"; 
	}
	
	//display basic character data
	echo "Character info: </p>";
	echo "<div class='mainPage'>";
	echo "<table>";
	echo "<tr><th>Character name: </th><th>$name</th></tr>";
	//links to other sub-pages
	//character stats
	echo "<tr><th colspan='2'><a class='reg' href='charstats.php?char=$char&realm=$realmSpecial' title='Click here for character stats like stamina, agility etc.'>Basic character stats</a></th></tr>";
	//items
	echo "<tr><th colspan='2'><a class='reg' href='itemstats.php?char=$char&realm=$realmSpecial' title='Click here for item stats like crit %, dps etc.'>Basic Item-related stats</a></th></tr>";
	//echo "<tr><th colspan='2'><a href='stats.php' title='Click here for general stats like kills, quests etc.'>Character Stats</a></th></tr>";
	if (!empty($guildName))//only show the guild option if the guild is set
	{
	echo "<tr><th colspan='2'><a class='reg' href='guild.php?guild=$guild&realm=$realmSpecial' title='Click here for guild information like race and class breakdown'>Basic guild stats</a></th></tr>";
	}
	//basic data
	echo "<tr><td>Level</td><td title='$lvlGraph'>$levelLink</td></tr>";
	echo "<tr><td>Item Level</td><td title='$lvlGraph'>$ilevelLink</td></tr>";
	echo "<tr><td>Guild</td><td>$guildName</td></tr>";
	echo "<tr><td>Faction</td><td>$faction</td></tr>";
	echo "<tr><td>Race</td><td>$race</td></tr>";
	echo "<tr><td>Class</td><td>$class</td></tr>";
	echo "<tr><td>Last Login</td><td>$lastLogin</td></tr>";
	echo "<tr><td>Achievement points</td><td>$points</td></tr>";
	echo "<tr><td>Specialization</td><td>$talent1</td></tr>";
	echo "<tr><td>Secondary</td><td>$talent2</td></tr>";

	//show professions
	$i=0;
	foreach ($prof as $profName)
	{
		list ($expected, $colour, $cap) = profTarget($profName,$level,$profval[$i]);//get variable values from function
		
		$class = $colour;//set the element colour
		if ($cap > 0)
			{$title = 'Capped until level: ' . $cap;}//if capped, display the level
		else
			{$title = 'Expected level: ' . $expected;}//else display the expected level
		echo "<tr>","<td>","Profession ",($i+1),"</td><td title='",$title,"' class='",$class,"'>",$profName,"(",$profval[$i],"/",$profmax[$i],")</td></tr>";//output row
		$i++;
	}

	//show secondary skills
	$i=0;
	foreach ($skill as $skillName)
	{
		list ($expected, $colour, $cap) = profTarget($skillName,$level,$skillval[$i]);//get variable values from function
		$class = $colour;//set the element colour
		if ($cap > 0)
			{$title = 'Capped until level: ' . $cap;}//if capped, display the level
		else
			{$title = 'Expected level: ' . $expected;}//else display the expected level
		echo "<tr>","<td>","Secondary Skill </td><td title='",$title,"' class='", $class,"'>",$skillName," (",$skillval[$i],"/",$skillmax[$i],")</td></tr>";
		$i++;
	}
	//end professions and skills
	echo "</table>";
	/*NEED TO INSERT A LEGEND HERE
echo "<table class='legend'>";
echo "<tr><th colspan=2>Legend</th></tr>";

echo "</table>";
*/
	echo "</div>";


	echo "<br/>";
	echo "<a class='reg' href='http://www.allmyrandomstuff.com/wow/index.php'>Search again</a> <br/>";
	echo "</p>";

}//end character lookup

else if (empty($_GET['char']))
	{
		//form is not set, so display it
		//make a request to fill the form options
		include 'api.php';
		
		//display character input form
		echo "<div class='login'>";
		echo "<form name='char1' method='get'>";
		echo "<p>Search for a character:<br/><br/>";
		echo "Character: <input type='text' name='char'><br/><br/>";
		echo "Realm: <select name='realm'>";
		// Get all realm statuses
		try
		{
			$allRealms = $api->getRealmApi()->getRealms();
		}
		catch (exception $e)
		{
			echo 'Error retrieving realm data: ', $e->getMessage();
			include 'footer.html';
			exit;
		}

		//count all realms
		$i=0;
			foreach ($allRealms as $realm)
			{
				if ($realm['status']==1)
					//if the realm is online output it as a dropdown option
					{
					echo '<option value="' . $realm['name'].'">'.$realm['name'].'</option>';
				}
				$i++;
			}
		echo "</select><br/>Please note, only online realms are shown<br/><br/>";
		echo "<input type='submit' value='Search'/><br/>";
	
		
		echo "</p>";
		echo "</form>";
		echo "</div>";

	}


if ($ua['msie']) 
{
echo "<p>";
echo "This page is not designed for Internet Explorer.  If you want to see this webpage as intended, 
please use a standards compliant browser, such as <a href='http://www.google.com/chrome'>Google Chrome</a>.";
echo "</p>";
}

include 'footer.html';
?>
